<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>pbliteserializer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_proto2_pbliteserializer.js.html">pbliteserializer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2008 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Protocol Buffer 2 Serializer which serializes messages
<a name="line17"></a> *  into PB-Lite (&quot;JsPbLite&quot;) format.
<a name="line18"></a> *
<a name="line19"></a> * PB-Lite format is an array where each index corresponds to the associated tag
<a name="line20"></a> * number. For example, a message like so:
<a name="line21"></a> *
<a name="line22"></a> * message Foo {
<a name="line23"></a> *   optional int bar = 1;
<a name="line24"></a> *   optional int baz = 2;
<a name="line25"></a> *   optional int bop = 4;
<a name="line26"></a> * }
<a name="line27"></a> *
<a name="line28"></a> * would be represented as such:
<a name="line29"></a> *
<a name="line30"></a> * [, (bar data), (baz data), (nothing), (bop data)]
<a name="line31"></a> *
<a name="line32"></a> * Note that since the array index is used to represent the tag number, sparsely
<a name="line33"></a> * populated messages with tag numbers that are not continuous (and/or are very
<a name="line34"></a> * large) will have many (empty) spots and thus, are inefficient.
<a name="line35"></a> *
<a name="line36"></a> *
<a name="line37"></a> */
<a name="line38"></a>
<a name="line39"></a>goog.provide(&#39;goog.proto2.PbLiteSerializer&#39;);
<a name="line40"></a>
<a name="line41"></a>goog.require(&#39;goog.proto2.Descriptor&#39;);
<a name="line42"></a>goog.require(&#39;goog.proto2.FieldDescriptor&#39;);
<a name="line43"></a>goog.require(&#39;goog.proto2.LazyDeserializer&#39;);
<a name="line44"></a>goog.require(&#39;goog.proto2.Util&#39;);
<a name="line45"></a>goog.require(&#39;goog.string&#39;);
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>
<a name="line49"></a>/**
<a name="line50"></a> * PB-Lite serializer.
<a name="line51"></a> *
<a name="line52"></a> * @constructor
<a name="line53"></a> * @extends {goog.proto2.LazyDeserializer}
<a name="line54"></a> */
<a name="line55"></a>goog.proto2.PbLiteSerializer = function() {};
<a name="line56"></a>goog.inherits(goog.proto2.PbLiteSerializer, goog.proto2.LazyDeserializer);
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>/**
<a name="line60"></a> * Serializes a message to a PB-Lite object.
<a name="line61"></a> *
<a name="line62"></a> * @param {goog.proto2.Message} message The message to be serialized.
<a name="line63"></a> *
<a name="line64"></a> * @return {Object} The serialized form of the message.
<a name="line65"></a> */
<a name="line66"></a>goog.proto2.PbLiteSerializer.prototype.serialize = function(message) {
<a name="line67"></a>  var descriptor = message.getDescriptor();
<a name="line68"></a>  var fields = descriptor.getFields();
<a name="line69"></a>
<a name="line70"></a>  var serialized = [];
<a name="line71"></a>
<a name="line72"></a>  // Add the known fields.
<a name="line73"></a>  for (var i = 0; i &lt; fields.length; i++) {
<a name="line74"></a>    var field = fields[i];
<a name="line75"></a>
<a name="line76"></a>    if (!message.has(field)) {
<a name="line77"></a>      continue;
<a name="line78"></a>    }
<a name="line79"></a>
<a name="line80"></a>    var tag = field.getTag();
<a name="line81"></a>
<a name="line82"></a>    if (field.isRepeated()) {
<a name="line83"></a>      serialized[tag] = [];
<a name="line84"></a>
<a name="line85"></a>      for (var j = 0; j &lt; message.countOf(field); j++) {
<a name="line86"></a>        serialized[tag][j] =
<a name="line87"></a>            this.getSerializedValue(field, message.get(field, j));
<a name="line88"></a>      }
<a name="line89"></a>    } else {
<a name="line90"></a>      serialized[tag] = this.getSerializedValue(field, message.get(field));
<a name="line91"></a>    }
<a name="line92"></a>  }
<a name="line93"></a>
<a name="line94"></a>  // Add any unknown fields.
<a name="line95"></a>  message.forEachUnknown(function(tag, value) {
<a name="line96"></a>    serialized[tag] = value;
<a name="line97"></a>  });
<a name="line98"></a>
<a name="line99"></a>  return serialized;
<a name="line100"></a>};
<a name="line101"></a>
<a name="line102"></a>
<a name="line103"></a>/** @inheritDoc */
<a name="line104"></a>goog.proto2.PbLiteSerializer.prototype.deserializeField =
<a name="line105"></a>  function(message, field, value) {
<a name="line106"></a>
<a name="line107"></a>   if (value == null) {
<a name="line108"></a>     return null;
<a name="line109"></a>   }
<a name="line110"></a>
<a name="line111"></a>   if (field.isRepeated()) {
<a name="line112"></a>     var data = [];
<a name="line113"></a>
<a name="line114"></a>     goog.proto2.Util.assert(goog.isArray(value));
<a name="line115"></a>
<a name="line116"></a>     for (var i = 0; i &lt; value.length; i++) {
<a name="line117"></a>       data[i] = this.getDeserializedValue(field, value[i]);
<a name="line118"></a>     }
<a name="line119"></a>
<a name="line120"></a>     return data;
<a name="line121"></a>   } else {
<a name="line122"></a>     return this.getDeserializedValue(field, value);
<a name="line123"></a>   }
<a name="line124"></a>};
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/** @inheritDoc */
<a name="line128"></a>goog.proto2.PbLiteSerializer.prototype.getSerializedValue =
<a name="line129"></a>  function(field, value) {
<a name="line130"></a>  if (field.getFieldType() == goog.proto2.Message.FieldType.BOOL) {
<a name="line131"></a>    // Booleans are serialized in numeric form.
<a name="line132"></a>    return value ? 1 : 0;
<a name="line133"></a>  }
<a name="line134"></a>
<a name="line135"></a>  return goog.proto2.Serializer.prototype.getSerializedValue.apply(this,
<a name="line136"></a>                                                                   arguments);
<a name="line137"></a>};
<a name="line138"></a>
<a name="line139"></a>/** @inheritDoc */
<a name="line140"></a>goog.proto2.PbLiteSerializer.prototype.getDeserializedValue =
<a name="line141"></a>  function(field, value) {
<a name="line142"></a>
<a name="line143"></a>  if (field.getFieldType() == goog.proto2.Message.FieldType.BOOL) {
<a name="line144"></a>    // Booleans are serialized in numeric form.
<a name="line145"></a>    return value === 1;
<a name="line146"></a>  }
<a name="line147"></a>
<a name="line148"></a>  return goog.proto2.Serializer.prototype.getDeserializedValue.apply(this,
<a name="line149"></a>                                                                     arguments);
<a name="line150"></a>};
</pre>


</body>
</html>
